<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="节点名称" v-model="listQuery.nodename"> </el-input>
    <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
    <el-button class="filter-item"  v-if="userManager_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
  </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
    <el-table-column align="center" label="节点ID" width="90"> <template scope="scope">
          <span>{{scope.row.nodeid}}</span>
        </template> </el-table-column>
    <el-table-column width="150" align="center" label="节点名称"> <template scope="scope">
        <span>{{scope.row.nodename}}</span>
      </template> </el-table-column>
    <el-table-column width="150" align="center" label="节点IP"> <template scope="scope">
            <span>{{scope.row.nodeip}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="节点功能"> 
      <template v-if="scope.row.nodetype!=null" scope="scope" >
        <span v-for="item in scope.row.nodetype" v-bind:key="item">
          <span v-for="type in nodeTypeOptions" v-bind:key="type.key">
            <el-row v-if="type.key==item">
              <span>{{type.name}}</span>
            </el-row>
          </span>
        </span>
      </template> 
    </el-table-column>
    <el-table-column width="100" align="center" label="节点状态" prop="status">
      <template scope="scope">
        <span v-if="scope.row.status=='01'">在线</span>
        <span v-if="scope.row.status=='02'">挂起</span>
      </template>
    </el-table-column>
    <el-table-column width="180" align="center" label="创建时间"> 
      <template scope="scope">
        <span>{{scope.row.createtime}}</span>
      </template> 
    </el-table-column>
    <el-table-column align="center" label="操作" > <template scope="scope">
        <el-button v-if="userManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button v-if="userManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
        </el-button>
      </template> </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="节点名称" prop="nodename">
        <el-input v-model="form.nodename" placeholder="请输入节点名称"></el-input>
      </el-form-item>
      <el-form-item label="节点IP" prop="nodeip">
        <el-input v-model="form.nodeip" placeholder="请输入节点IP"></el-input>
      </el-form-item>
      <!-- <el-form-item label="eth生成的节点" prop="nodenumber">
        <el-input v-model="form.nodenumber" placeholder="请输入eth生成的节点"></el-input>
      </el-form-item> -->
      <el-form-item label="以太节点地址" prop="nodeurl">
        <el-input v-model="form.nodeurl" placeholder="请输入以太节点地址"></el-input>
      </el-form-item>
      <el-form-item label="节点功能" prop="nodetype">
        <!-- <el-input v-model="form.nodetype" placeholder="请输入节点功能"></el-input> -->
        <el-select class="filter-item" v-model="form.nodetype" multiple collapse-tags  value-key="key" placeholder="节点功能"  scope="scope" >
          <el-option v-for="item in nodeTypeOptions" :key="item.key" :label="item.name" :value="item.key" ></el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="节点状态" prop="status">
        <el-select class="filter-item" v-model="form.status" value-key="key" placeholder="请输入节点状态" scope="scope">
          <el-option v-for="item in statusOptions" :key="item.key" :label="item.name" :value="item.key"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj
} from 'api/blockchain/node/index';
import { mapGetters } from 'vuex';
export default {
  name: 'node',
  data() {
    return {
      form: {
        nodeid: undefined,
        nodename: undefined,
        nodenumber: undefined,
        nodeip: undefined,
        nodetype: undefined,
        status: '02',
        createtime: undefined,
        updatetime: undefined,
        nodeurl: undefined
      },
      rules: {
        nodename: [
          {
            required: true,
            message: '请输入节点名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        nodeip: [
          {
            required: true,
            message: '请输入节点IP',
            trigger: 'blur'
          },
          { 
            pattern: /(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)/, 
            message: '请输入正确的IP地址' 
          }
        ],
        nodetype: [
          {
            type: 'array',
            // required: true,
            message: '请选择节点类型',
            // trigger: 'blur'
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择节点状态',
            trigger: 'blur'
          }
        ],
        nodeurl: [
          {
            required: true,
            message: '请输入节点URL',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined
      },
      statusOptions: [
        {key:'01',name:'在线'},
        {key:'02',name:'挂起'}
      ],
      nodeTypeOptions: [
        {key:'01',name:'交易节点'},
        {key:'02',name:'挖矿节点'},
        {key:'03',name:'普通块节点'},
        {key:'04',name:'数据节点'},
        {key:'05',name:'管理节点'}
      ],
      dialogFormVisible: false,
      dialogStatus: '',
      userManager_btn_edit: false,
      userManager_btn_del: false,
      userManager_btn_add: false,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableKey: 0
    }
  },
  created() {
    this.getList();
    this.userManager_btn_edit = this.elements['userManager:btn_edit'];
    this.userManager_btn_del = this.elements['userManager:btn_del'];
    this.userManager_btn_add = this.elements['userManager:btn_add'];
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getList() {
      this.listLoading = true;
      page(this.listQuery)
        .then(response => {
          var dataLists = response.data.rows;
          var list = this.formatNodeTypes(dataLists);
          this.list = list;
          this.total = response.data.total;
          this.listLoading = false;
        })
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      getObj(row.nodeid)
        .then(response => {
          var row = response.data;
          this.form = this.formatNodeType(row);
          this.dialogFormVisible = true;
          this.dialogStatus = 'update';
        });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row.nodeid)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        });
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          var nodeType = this.form.nodetype;
          this.form.nodetype = this.reqNodeTypeFormat(nodeType);
          addObj(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
            })
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          var nodeType = this.form.nodetype;
          this.form.nodetype = this.reqNodeTypeFormat(nodeType);
          putObj(this.form.nodeid, this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    reqNodeTypeFormat(nodeType){
      var typesStr = '';
      for(var i=0 ;i<nodeType.length;i++){
        typesStr = typesStr + nodeType[i] +'#';
      }
      return typesStr;
    },
    formatNodeTypes(dataLists){
      var list = [];
      for(var i=0; i<dataLists.length ; i++){
        var row = dataLists[i];
        list.push(this.formatNodeType(row));
      }
      return list;
    },
    formatNodeType(row){
      var nodetype = row.nodetype;
      var nodeList = [];
      if(nodetype==null){
        row.nodetype = nodeList;
        return row;
      }
      for(var a=0; a<this.nodeTypeOptions.length ; a++){
        var index = nodetype.indexOf(this.nodeTypeOptions[a].key+'#');
        if(index!=-1){
          nodeList.push(this.nodeTypeOptions[a].key);
        }
      }
      row.nodetype = nodeList;
      return row;
    },
    resetTemp() {
      this.form = {
        nodeid: undefined,
        nodename: undefined,
        nodenumber: undefined,
        nodeip: undefined,
        nodetype: ['01'],
        status: '02',
        createtime: undefined,
        updatetime: undefined,
        nodeurl: undefined
      };
    }
  }
}
</script>
